package com.quectel.core.module.grid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.grid.dao.GridBindingDao;
import com.quectel.core.module.grid.dto.GridBindingDto;
import com.quectel.core.module.grid.entity.GridBindingEntity;
import com.quectel.core.module.grid.service.GridBindingService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/02/03 15:45
 */
@DubboService
public class GridBindingServiceImpl implements GridBindingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GridBindingServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> GRID_BINDING_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "GRID_BINDING_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<GridBindingDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(GRID_BINDING_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private GridBindingDao gridBindingDao;


    /**
     * @param sourceType
     * @param sourceId
     * @return
     */
    @Override
    public GridBindingDto selectOne(Byte sourceType, Long sourceId) {
        GridBindingEntity entity = gridBindingDao.selectOne(
                new LambdaQueryWrapper<GridBindingEntity>()
                        .eq(GridBindingEntity::getSourceType, sourceType)
                        .eq(GridBindingEntity::getSourceId, sourceId)
        );
        return paddingField(CopyUtils.copyObj(entity, GridBindingDto.class));
    }

    /**
     * @param sourceType
     * @param gridId
     * @return
     */
    @Override
    public List<GridBindingDto> selectListByGridIdAndSourceType(Long gridId, Byte sourceType) {
        List<GridBindingEntity> list = gridBindingDao.selectList(new LambdaQueryWrapper<GridBindingEntity>()
                .eq(GridBindingEntity::getGridId, gridId)
                .eq(GridBindingEntity::getSourceType, sourceType)
        );
        List<GridBindingDto> result = CopyUtils.copyList(list, GridBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public GridBindingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                GRID_BINDING_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public GridBindingDto selectById(Long id) {
        GridBindingEntity entity = gridBindingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, GridBindingDto.class));
    }

    @Override
    public List<GridBindingDto> queryList(Map<String, Object> params) {
        List<GridBindingEntity> list = gridBindingDao.queryList(params);
        List<GridBindingDto> result = CopyUtils.copyList(list, GridBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属社区等
     *
     * @param dto
     * @return
     */
    private GridBindingDto paddingField(GridBindingDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return gridBindingDao.queryTotal(params);
    }

    @Override
    public Long save(GridBindingDto dto) {
        GridBindingEntity entity = CopyUtils.copyObj(dto, GridBindingEntity.class);
        entity.setId(Snowflake.nextId());
        gridBindingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(GridBindingDto dto) {
        GridBindingEntity entity = CopyUtils.copyObj(dto, GridBindingEntity.class);

        gridBindingDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(GridBindingDto dto) {
        GridBindingEntity entity = CopyUtils.copyObj(dto, GridBindingEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, gridBindingDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        gridBindingDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
